﻿Imports System.Windows.Forms
Imports DotSpatial.Data, DotSpatial.Symbology

Public Class frmCreateGrid

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Me.Hide()
        Dim fn As String = GetSetting(REGAPP, Me.Name, "Filename")
        With New SaveFileDialog
            '.InitialDirectory = GetSetting(REGAPP, Me.Name, "AddLayer", GISForm.LayerDirectory)
            .Filter = DotSpatial.Data.DataManager.DefaultDataManager.VectorWriteFilter
            .AddExtension = True
            .DefaultExt = ".shp"
            .OverwritePrompt = True
            .Title = "Create Grid Layer"
            If Not String.IsNullOrEmpty(fn) Then
                .InitialDirectory = IO.Path.GetDirectoryName(fn)
                .FileName = IO.Path.GetFileName(fn)
            End If
            If .ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
                fn = IO.Path.ChangeExtension(.FileName, ".shp")
                WriteProgress(GISForm, "Creating grid...")
                With New clsGrid(GISForm.GetLayer(cboLayerSource.Text), cboField.Text, fn, cboCellSize.SelectedIndex, udCellSize.Value, udNumPts.Value, Val(txtMaxDist.Text), udExponent.Value, udNumCat.Value, GISForm.GetLayer(cboLayerClip.Text))
                    Dim lyr = .CreateGrid()
                    If lyr Is Nothing Then Exit Sub
                    WriteProgress(GISForm, "Computing spatial weights...")
                    .ComputeWeights()
                    GISForm.MoveBelowPolylineLayers(lyr)
                    .lyrGrid = lyr 'gets reassigned in above method?
                    WriteProgress(GISForm, "Updating grid using inverse distance method...")
                    .UpdateGrid()
                    WriteProgress(GISForm, "Creating grid color scheme...")
                    .CreateScheme()
                    WriteProgress(GISForm)
                End With

                GISForm.Refresh()
                GISForm.GISLegend.Refresh()
            End If
            SaveSetting(REGAPP, Me.Name, "Filename", .FileName)
        End With

        SaveControlValues(REGAPP, Me)

        If WRDBProject IsNot Nothing AndAlso chkResetProjectClippingLayer.Visible AndAlso chkResetProjectClippingLayer.Checked Then
            WRDBProject.GisClippingLayerFilename = CType(GISForm.GetLayer(cboLayerClip.Text), PolygonLayer).DataSet.Filename
            WRDBProject.SavePreferences()
        End If

        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub cboLayerSource_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboLayerSource.SelectedIndexChanged
        If cboLayerSource.SelectedIndex <> -1 Then
            With cboField.Items
                .Clear()
                Dim fs As DotSpatial.Data.FeatureSet = GetFeatureSet(GISForm.GISMap, cboLayerSource.Text)
                For i As Integer = 0 To fs.DataTable.Columns.Count - 1
                    Dim colType As Type = fs.DataTable.Columns(i).DataType
                    Dim FieldName As String = fs.DataTable.Columns(i).ColumnName
                    If colType = GetType(System.Double) Or colType = GetType(System.Single) Or colType = GetType(System.Int32) Then .Add(FieldName)
                Next
                If cboField.Text = "" And cboField.Items.Count > 0 Then cboField.SelectedIndex = 0
            End With
            chkResetProjectClippingLayer.Visible = (WRDBProject IsNot Nothing AndAlso cboLayerSource.Text = WRDBGridLayerName)
        End If
    End Sub

    Private Sub frmCreateGrid_Load(sender As Object, e As EventArgs) Handles Me.Load
        For i = 1 To 2
            Dim cbo As ComboBox = Choose(i, cboLayerSource, cboLayerClip)
            With cbo.Items
                .Clear()
                For Each lyr In GISForm.GISMap.GetPolygonLayers
                    If Not String.IsNullOrEmpty(lyr.LegendText) Then .Add(lyr.LegendText)
                Next
                For Each lyr In GISForm.GISMap.GetPointLayers
                    If Not String.IsNullOrEmpty(lyr.LegendText) Then .Add(lyr.LegendText)
                Next
                If cbo.Items.Contains(Choose(i, WRDBStationsLayerName, WRDBGridLayerName)) Then
                    cbo.Text = Choose(i, WRDBStationsLayerName, WRDBGridLayerName)
                Else
                    If .Count > 0 Then cbo.SelectedIndex = 0
                End If
            End With
        Next
        udNumPts.Value = 5
        udCellSize.Value = 50
        txtMaxDist.Text = 500
        udNumCat.Value = 50

        GetControlValues(REGAPP, Me)
    End Sub
End Class
